package com.space.school.controller;

import com.space.school.domain.Info;
import com.space.school.domain.User;
import com.space.school.service.InfoService;
import com.space.school.service.impl.InfoServiceImpl;
import com.space.school.utils.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "InfoServlet",value = "/InfoServlet")
public class InfoServlet extends HttpServlet {
    private InfoService infoService=new InfoServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        设置编码格式
        req.setCharacterEncoding("UTF-8");
//        获取当前页数
        String currentPage=req.getParameter("currentPage");
//        如果第一次进页面,当前页数为空或为null
//        如果进页面的时候PB没有传进去,所以获取不到值就为null
        if(currentPage==null || currentPage.equals("")) {
            currentPage="1";
        }
        int pageSize=3;
        Map<String,String[]> map=req.getParameterMap();
        PageBean<Info> pb=infoService.search(map,Integer.parseInt(currentPage), pageSize);
//        将查询结果放入集合
        req.setAttribute("PB",pb);
        req.setAttribute("product_name",req.getParameter("product_name"));
        req.setAttribute("product_status",req.getParameter("product_status"));
        req.getRequestDispatcher("Info.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        Map<String,String[]>map=req.getParameterMap();
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null || currentPage.equals("")) {
            currentPage="1";
        }
        int pageSize=3;
        PageBean<Info>infoList = infoService.search(map, Integer.parseInt(currentPage),pageSize);
        req.setAttribute("PB",infoList);
        req.setAttribute("product_name",req.getParameter("product_name"));
        String product_status=req.getParameter("product_status");
        if(!product_status.equals("") && product_status!=null) {
            req.setAttribute("product_status",req.getParameter("product_status"));
        }

        req.getRequestDispatcher("Info.jsp").forward(req,resp);


    }
}
